<html>
<head><meta charset="utf-8"><title>What heuristics are used to report failed trait bounds? · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20heuristics.20are.20used.20to.20report.20failed.20trait.20bounds.3F.html">What heuristics are used to report failed trait bounds?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="236548330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20heuristics%20are%20used%20to%20report%20failed%20trait%20bounds%3F/near/236548330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20heuristics.20are.20used.20to.20report.20failed.20trait.20bounds.3F.html#236548330">(Apr 28 2021 at 17:38)</a>:</h4>
<p>In this example, the compiler reports that <code>Vec&lt;&amp;&amp;str&gt;</code> doesn't implement <code>Iterator</code> (needed by <code>Itertools::join</code>). That's <em>true</em>, but in this case the user wanted to call <code>slice::join</code> (where the bounds are <strong>also</strong> not met). How/why does the compiler decide to show one set of failed bounds but not the other?</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">itertools</span>::<span class="n">Itertools</span><span class="p">;</span><span class="w"> </span><span class="c1">// 0.10.0</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">combos</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="s">"a"</span><span class="p">,</span><span class="w"> </span><span class="s">"b"</span><span class="p">,</span><span class="w"> </span><span class="s">"c"</span><span class="p">].</span><span class="n">iter</span><span class="p">().</span><span class="n">combinations</span><span class="p">(</span><span class="mi">2</span><span class="p">).</span><span class="n">collect</span>::<span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="n">_</span><span class="o">&gt;&gt;</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{:#?}"</span><span class="p">,</span><span class="w"> </span><span class="n">combos</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">join</span><span class="p">(</span><span class="s">""</span><span class="p">));</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>error[E0599]: the method `join` exists for struct `Vec&lt;&amp;&amp;str&gt;`, but its trait bounds were not satisfied
   --&gt; src/main.rs:5:33
    |
5   |       println!(&quot;{:#?}&quot;, combos[0].join(&quot;&quot;));
    |                                   ^^^^ method cannot be called on `Vec&lt;&amp;&amp;str&gt;` due to unsatisfied trait bounds
    |
    = note: the following trait bounds were not satisfied:
            `Vec&lt;&amp;&amp;str&gt;: Iterator`
            which is required by `Vec&lt;&amp;&amp;str&gt;: Itertools`
            `&lt;[&amp;&amp;str] as Join&lt;_&gt;&gt;::Output = _`
            `[&amp;&amp;str]: Iterator`
            which is required by `[&amp;&amp;str]: Itertools`
</code></pre></div>



<a name="236549296"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20heuristics%20are%20used%20to%20report%20failed%20trait%20bounds%3F/near/236549296" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> The 8472 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20heuristics.20are.20used.20to.20report.20failed.20trait.20bounds.3F.html#236549296">(Apr 28 2021 at 17:45)</a>:</h4>
<p>sorry, wrong thread</p>



<a name="236651314"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20heuristics%20are%20used%20to%20report%20failed%20trait%20bounds%3F/near/236651314" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20heuristics.20are.20used.20to.20report.20failed.20trait.20bounds.3F.html#236651314">(Apr 29 2021 at 10:33)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> I'd say it favors methods for the very type in question, even behind traits (do they have to be in scope?), and maybe even <code>&amp;self</code> and <code>&amp;mut self</code> methods. But in order to think of <code>&lt;[_]&gt;::join</code> it would need to go through a <code>Deref</code>, which I don't think can happen when wanting to suggest methods.</p>



<a name="236777331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20heuristics%20are%20used%20to%20report%20failed%20trait%20bounds%3F/near/236777331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20heuristics.20are.20used.20to.20report.20failed.20trait.20bounds.3F.html#236777331">(Apr 30 2021 at 02:38)</a>:</h4>
<p>It seems like it already deref'd though in the note about which trait bounds were not satisfied.</p>
<div class="codehilite"><pre><span></span><code>`&lt;[&amp;&amp;str] as Join&lt;_&gt;&gt;::Output = _`
`[&amp;&amp;str]: Iterator`
</code></pre></div>



<a name="236825291"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/What%20heuristics%20are%20used%20to%20report%20failed%20trait%20bounds%3F/near/236825291" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/What.20heuristics.20are.20used.20to.20report.20failed.20trait.20bounds.3F.html#236825291">(Apr 30 2021 at 12:16)</a>:</h4>
<p><span class="user-mention" data-user-id="307537">@Camelid</span> oh you're right, I somehow missed that line; so it did happen to mention <code>[_] as Join</code>, although not very "loudly". Also, <code>.join()</code> used to be a trait-enabled method and is now an inherent method that is trait-guarded.<br>
If anything, the <code>&lt;[&amp;&amp;str] as Join&lt;_&gt;&gt;::Output = _</code> line ought to be spelled as <code>[&amp;&amp;str] : Join&lt;&amp;str&gt;</code>, and from there complain that <code>&amp;&amp;str : Borrow&lt;str&gt;</code> is not met.</p>
<p>In other words, the following error message is more like what I would have expected:</p>
<div class="codehilite" data-code-language="Text only"><pre><span></span><code>    = note: the following trait bounds were not satisfied:
            `Vec&lt;&amp;&amp;str&gt;: Iterator`
            which is required by `Vec&lt;&amp;&amp;str&gt;: Itertools`
            `&amp;&amp;str : Borrow&lt;str&gt;`
            which is required by `[&amp;&amp;str] : Join&lt;&amp;str&gt;`
            `[&amp;&amp;str]: Iterator`
            which is required by `[&amp;&amp;str]: Itertools`
</code></pre></div>
<p>So it does not seem that much to be about order of resolution (no indirection (inherent then through trait), followed by through deref indirection (inherent then through trait)), but rather, about the readbility of the unmet bounds for <code>Join</code> to be met, I'd say.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>